import {reactive, SetupContext} from "vue";
import {FormOptions} from "@/interface/FormInterface";

export function useFormOptionsToFormData(formOptions: FormOptions[], ctx?: SetupContext) {
    let formData: any = {}
    formOptions.forEach(item => {
        formData[item.name] = item.value || '';
    })
    formData = reactive(formData)
    const onReset = () => {
        for (let key in formData) {
            formData[key] = formData[key] || '';
        }
    }
    const onConfirm = () => {
        if (ctx) ctx.emit('onConfirm', formData)
    }
    return {
        formOptions,
        formData,
        onReset,
        onConfirm,
    }
}
